<template>
  <div>
    <div class="emissionSourceFactor">
      <!-- 标题 -->
      <div class="emissionSourceFactor-title">排放因子</div>


      <!-- 条件筛选 -->
      <div class="-condition">
        <div class="emissionSourceFactor-condition-form">
          <normal-form-plus
            :formModel="formModel"
            :config="config"
            :plus="plus"/>
        </div>

      </div>
      <div class="emissionSourceFactor-condition-btn">
        <el-button @click="getEmissionFactorByPage()">查询</el-button>
        <el-button @click="reset()">重置</el-button>
        <el-button @click="addSure()">确认添加</el-button>
      </div>


      <!-- 表格-核算报告 -->
      <div class="emissionSourceFactor-report">
        <normal-table
          ref="myTable"
          :tableData="tableData"
          :paginationConfig="paginationConfig"
          :tableLabel="tableLabel"
          :operationConfig="operationConfig"
          :operationList="operationList"
          @event="handleEvent"
          @changePage="changePage"></normal-table>
      </div>


    </div>
  </div>
</template>

<script>
import NormalTable from '@/components/NormalTable'
import NormalFormPlus from '@/components/NormalFormPlus'

export default {
  name: 'emissionSourceFactor',
  components: {
    NormalTable,
    NormalFormPlus
  },
  mounted() {
    this.getEmissionFactorByPage()
    // 来自产品碳足迹
    if (this.$route.query.cabon) {
      this.cabon = true
    }
  },
  data() {
    return {
      cabon: false,
      formModel: {
        name: "",
        year: ""
      },
      config: [
        {
          id: 1,
          items: [
            {
              id: '1',
              edit: true,
              type: 'input',
              span: 4,
              model: 'name',
              name: '',
              props: {
                placeholder: "因子名称",
                width: 150
              }
            },
            {
              id: '3',
              edit: true,
              type: 'input',
              span: 4,
              model: 'year',
              name: '',
              props: {
                placeholder: "发布年份",
              }
            },
          ]
        },
      ],
      plus: {},
      tableData: [],
      paginationConfig: {
        total: 100,
        page: 1,
        pageSize: 10,
        show: true
      },
      operationList: [
        {
          name: '详情',
          order: 1,
          buttonClass: 'primary',
          plus: {}
        }
      ],
      operationConfig: {select: true},
      tableLabel: [
        {
          prop: "order",
          label: "序号",
          width: 70
        },
        {
          prop: "name",
          label: "因子名称",
          width: 200
        },
        {
          prop: "value",
          label: "因子数量",
        },
        {
          prop: "unitD",
          label: "单位",
        },
        {
          prop: "releaseYear",
          label: "发布年份",
        },
        {
          prop: "dataSourceSimple",
          label: "参考文献/数据来源",
        },

      ],

    }
  },
  methods: {
    test() {
      console.log(this.getSelectedFactor())
    },
    reset() {
      this.formModel = {
        name: "",
        year: ""
      },
        this.paginationConfig = {
          total: 100,
          page: 1,
          pageSize: 10,
          show: true
        },
        this.getEmissionFactorByPage()
    },
    // 获取选择中的排放因子
    getSelectedFactor() {
      var li = this.$refs.myTable.getSelection()
      return li
    },
    // 添加排放源字典
    addSure() {
      if (this.cabon) {
        var model = JSON.parse(window.sessionStorage.getItem("form"))
        console.log("get model")
        console.log(model)
        console.log(model.factor)
        if (model) {
          var li = this.getSelectedFactor()
          model.factor = li[0].name
          window.sessionStorage.setItem("form", JSON.stringify(model))
          this.$router.push({
            path: "/carbonFootprintVerification/carbonFootprintVerification/emissionSource",
            query: {storage: true}
          })
        }
      }
      // 添加元素前设置相关值
      var model = JSON.parse(window.sessionStorage.getItem("form"))
      if (model) {
        var li = this.getSelectedFactor()
        for (var i = 0; i < li.length; i++) {
          model.formModel3.factor.push({
            gaseId: li[i].id,
            name: li[i].name,
            unitLeft: li[i].unitLeft,
            unitRight: li[i].unitRight,
            value: li[i].value,
            gwp: '-'
          })
        }

        window.sessionStorage.setItem("form", JSON.stringify(model))
        this.$router.push({
          path: "/enterpriseCarbonAccounting/carbonEmissionManage/addEmissionSourceDictionary",
          query: {storage: true}
        })
      } else {
        this.$router.push({path: "/enterpriseCarbonAccounting/carbonEmissionManage/addEmissionSourceDictionary"})
      }

    },
    // 处理表格点击事件
    handleEvent(order, row) {
      if (order === 1) {

      }
    },
    // 分页点击事件
    changePage() {
      this.getEmissionFactorByPage()
    },
    // 获取排放因子列表
    getEmissionFactorByPage() {
      this.$API.carbon.getEmissionFactorByPage({
        pageNo: this.paginationConfig.page,
        pageSize: this.paginationConfig.pageSize,
        level3c: this.formModel.name == "" ? null : this.formModel.name,
        releaseYear: this.formModel.year == "" ? null : this.formModel.year
      }).then(response => {
        // 数据预处理
        for (var i = 0; i < response.data.list.length; i++) {
          if (response.data.list[i].releaseYear) {
            response.data.list[i].releaseYear = response.data.list[i].releaseYear.substring(0, 4)
          }
          if (response.data.list[i].unitLeft && response.data.list[i].unitRight) {
            response.data.list[i].unitD = response.data.list[i].unitLeft + "/" + response.data.list[i].unitRight
          }
        }
        this.tableData = response.data.list
        this.paginationConfig.total = response.data.total
      })
    }
  }
}
</script>

<style lang="less" scoped>
.emissionSourceFactor {

  height: auto;
  overflow: hidden;
  padding: 15px 10px;
  // 标题
  .emissionSourceFactor-title {
    font-size: 16px;
    font-weight: bold;
  }

  .emissionSourceFactor-condition {
    display: flex;

    .emissionSourceFactor-condition-form {
      flex: 3;
    }

    .emissionSourceFactor-condition-btn {
      flex: 1;
    }
  }

  // 核算报告表格
  .emissionSourceFactor-report {
    .emissionSourceFactor-history-title {
      font-size: 14px;
    }
  }
}
</style>
